package day14.homework02.test01;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Xiao
 * @date 2021/8/7
 */
public class Ticket implements Runnable{
    private static int count = 100;
    private static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (count <= 0) {
                    break;
                }
                count--;
                System.out.println(Thread.currentThread().getName()
                        + "卖出一张票，还剩" + count + "张票。。");
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

}
